package com.shanghaionstar.utils;

import android.location.Location;
import android.os.Bundle;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.location.LocationManagerProxy;
import com.amap.api.location.LocationProviderProxy;
import com.onstar.cn.common.log.LogManager;

/**
 * @ClassName: LocationListenerProxy
 * @author Xiaofeng Feng
 * @date 2015-08-06 下午4:21:14
 */
public class LocationListenerProxy implements AMapLocationListener {
	private final LocationManagerProxy mLocationManager;
	private static LogManager logger = LogManager.getInstance();
	private AMapLocationListener mListener = null;

	public LocationListenerProxy(final LocationManagerProxy pLocationManager) {
		mLocationManager = pLocationManager;
	}

	public boolean startListening(final AMapLocationListener pListener, final long pUpdateTime, final float pUpdateDistance) {
		boolean result = false;
		mListener = pListener;
		// 获取当前可用的Provider,其中MapABCNetwork为MapABC网络定位(基站和WiFi)
		for (final String provider : mLocationManager.getProviders(true)) {
			if (LocationManagerProxy.GPS_PROVIDER.equals(provider)
					|| LocationProviderProxy.AMapNetwork.equals(provider)) {
				logger.d("provider=" + provider);
				result = true;
				mLocationManager.requestLocationUpdates(provider, pUpdateTime, pUpdateDistance, this);
			}
		}
		return result;
	}

	public void stopListening() {
		mListener = null;
		mLocationManager.removeUpdates(this);
		mLocationManager.removeUpdates(mListener);
		// mLocationManager.destory();
		logger.d("stopListening");
	}

	@Override
	public void onLocationChanged(AMapLocation arg0) {
		if (mListener != null) {
			mListener.onLocationChanged(arg0);
		}
	}

	@Override
	public void onLocationChanged(final Location location) {
		if (mListener != null) {
			mListener.onLocationChanged(location);
		}
	}

	@Override
	public void onProviderDisabled(final String arg0) {
		if (mListener != null) {
			mListener.onProviderDisabled(arg0);
		}
	}

	@Override
	public void onProviderEnabled(final String arg0) {
		if (mListener != null) {
			mListener.onProviderEnabled(arg0);
		}
	}

	@Override
	public void onStatusChanged(final String arg0, final int arg1, final Bundle arg2) {
		if (mListener != null) {
			mListener.onStatusChanged(arg0, arg1, arg2);
		}
	}

}
